package org.finesys.codegen.service.impl;

import cn.hutool.core.text.NamingCase;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.finesys.codegen.entity.GenFieldType;
import org.finesys.codegen.entity.GenTableColumn;
import org.finesys.codegen.enums.FormTypeEnum;
import org.finesys.codegen.mapper.GenFieldTypeMapper;
import org.finesys.codegen.mapper.GenTableColumnMapper;
import org.finesys.codegen.service.GenTableColumnService;
import org.finesys.common.mp.core.constants.QueryWrapperConstants;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class GenTableColumnServiceImpl extends BaseServiceImpl<GenTableColumnMapper, GenTableColumn> implements GenTableColumnService {

    private final GenFieldTypeMapper genFieldTypeMapper;

    @Override
    public void initFieldList(List<GenTableColumn> columnList) {
        //获取字段类型信息
        List<GenFieldType> fieldTypeList = genFieldTypeMapper.selectList(Wrappers.emptyWrapper());
        //转为map
        Map<String, GenFieldType> genFieldTypeMap = fieldTypeList.stream().collect(Collectors.toMap(s -> s.getColumnType().toLowerCase(), Function.identity(), (k1, k2) -> k1));
        //字段排序
        AtomicInteger index = new AtomicInteger(0);
        //对字段进行处理
        columnList.stream().forEach(genTableColumn -> {
            //字段名转为驼峰格式
            genTableColumn.setAttrName(NamingCase.toCamelCase(genTableColumn.getColumnName().toLowerCase()));
            //字段类型
            String columnType = genTableColumn.getColumnType().toLowerCase();
            GenFieldType genFieldType = genFieldTypeMap.getOrDefault(columnType, null);
            if (genFieldType == null) {
                genTableColumn.setAttrType("Object");
            } else {
                genTableColumn.setAttrType(genFieldType.getAttrType());
                genTableColumn.setPackageName(genFieldType.getPackageName());
            }
            //
            genTableColumn.setQueryType(QueryWrapperConstants.EQ);
            genTableColumn.setFormType(FormTypeEnum.TEXT.getFormType());
            genTableColumn.setQueryFormType(FormTypeEnum.TEXT.getFormType());
            //不为空字段取值原顺序
            Integer sort = genTableColumn.getSort();
            if (Objects.isNull(sort)) {
                sort = index.getAndIncrement();
            } else {
                //不为空时比较传递的值是否小于当前序号
                if (sort < index.get()) {
                    sort = index.getAndIncrement();
                }
            }
            genTableColumn.setSort(sort);
        });
    }


    @Override
    public void updateTableField(String dsName, String tableName, List<GenTableColumn> tableFieldList) {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        this.updateBatchById(tableFieldList.stream()
                .peek(field -> field.setSort(atomicInteger.getAndIncrement()))
                .collect(Collectors.toList()));
    }
}
